package com.maddox.relax.util;

import android.content.ContentUris;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import com.maddox.relax.helper.CameraCompat;
import com.maddox.relax.model.MimeType;

import java.io.File;

/**
 * 图片 文件 视频的处理
 */
public class PBitmapUtils {

    /**
     * 获取保存照片的app私有目录
     * 默认是在 /sdcard/Android/app包名/{@link com.maddox.relax.helper.CameraCompat#FILE_IMAGE_DIR}
     *
     * @param context 上下文
     * @return app私有目录
     */
    public static File getPickerFileDirectory(Context context) {
        File file = new File(context.getExternalFilesDir(null), CameraCompat.FILE_IMAGE_DIR);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }
    /**
     * 获取保存音视频的app私有目录
     * 默认是在 /sdcard/Android/app包名/{@link com.maddox.relax.helper.CameraCompat#FILE_VIDEO_DIR}
     *
     * @param context 上下文
     * @return app私有目录
     */
    public static File getVideoFileDirectory(Context context) {
        File file = new File(context.getExternalFilesDir(null), CameraCompat.FILE_VIDEO_DIR);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }


    /**
     * 获取图片的宽度高度
     *
     * @param path 图片指定路径
     * @return
     */
    public static int[] getImageWidthHeight(String path) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);
            return new int[]{options.outWidth, options.outHeight};
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new int[]{0, 0};
    }

    //获取指定类型以及文件id的uri路径
    public static Uri getContentUri(String mimeType, long id) {
        if (id <= 0) {
            return null;
        }
        Uri contentUri = null;
        if (MimeType.isImage(mimeType)) {
            contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        } else if (MimeType.isVideo(mimeType)) {
            contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        } else {
            contentUri = MediaStore.Files.getContentUri("external");
        }
        return ContentUris.withAppendedId(contentUri, id);

    }


}
